//
//  VistorLoginView.swift
//  微博项目
//
//  Created by zhaoyinpeng on 16/3/6.
//  Copyright © 2016年 zhaoyinpeng. All rights reserved.
//

import UIKit
import SnapKit

//.声明协议，需要继承基协议
@objc protocol VisitorLoginViewDelegate: NSObjectProtocol {
    
    //.声明协议方法  可选的协议方法
    optional func userWillRegister()
    
    //.登录的协议方法
    func userWillLognin()
}


class VistorLoginView: UIView {
    
    //.声明代理对象 代理的声明 在OC中必须使用 weak/assagin 为了防止循环引用
    weak var visitorDelegate: VisitorLoginViewDelegate?
    
    //.MARK: 外部调用的方法
    func setupInforWith(title: String,imageName: String?) {
        
        tioLabel.text = title
        
        
        if imageName != nil {
            //.不是首页
            cicrleIcon.image = UIImage(named: imageName!)
            //.隐藏房子, 遮罩
            largeHouse.hidden = true
            
            maskImage.hidden = true
        }else{
            startAnimation()
        }
        
        
        
    }
    
    //.设置首页圆圈的动画
    private func startAnimation() {
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = MAXFLOAT
        anim.duration = 10
        anim.toValue = 2 * M_PI
        
        //默认就是移除动画
        //.在动画结束  或者页面处于非活跃状态时，动画效果不移除图层
        anim.removedOnCompletion = false
        
        //.添加到图层
        cicrleIcon.layer.addAnimation(anim, forKey: nil)
        
        
    }
    
    
    
    //.自定义视图要先找到 入口
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //.访客视图自定义
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK :自定义访客视图
    private  func setUpUI() {
        
        addSubview(cicrleIcon)
        addSubview(maskImage)
        addSubview(largeHouse)
        addSubview(tioLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        
        //.布局 需要相对于一个已经约束好的视图来添加   链式布局
        //.圆圈
        cicrleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
            
            //    print(self.classForCoder)
        }
        //.房子
        largeHouse.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cicrleIcon.snp_center)
            
        }
        //首页文字
        tioLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(cicrleIcon.snp_centerX)
            make.centerY.equalTo(cicrleIcon.snp_centerY).offset(80)
            make.width.equalTo(230)
            make.height.equalTo(40)
            
            
        }
        //.登录， 注册布局
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX).offset(-70)
            make.centerY.equalTo(self.snp_centerY).offset(80)
            make.width.equalTo(100)
        }
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX).offset(70)
            make.centerY.equalTo(self.snp_centerY).offset(80)
            make.width.equalTo(100)
            
        }
        maskImage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cicrleIcon.snp_center)
            
            
        }
        
        let value: CGFloat = 236.0 / 255.0
        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: value)
        
        
        
        //.添加登录点击事件
        loginBtn.addTarget(self, action: "loginBtndidClick", forControlEvents: .TouchUpInside)
        
        registerBtn.addTarget(self, action: "registerClick", forControlEvents: .TouchUpInside)
        
    }
    
    //.点击登录调用
    func loginBtndidClick() {
        //        print(__FUNCTION__)
        
        //.执行闭包， 强制解包表示  闭包不能为空
        
        visitorDelegate?.userWillLognin()
    }
    
    func registerClick() {
        //        print(__FUNCTION__)
        visitorDelegate?.userWillRegister!()
    }
    
    //.懒加载视图
    
    //.遮罩
    private lazy var maskImage: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    
    //.圆圈
    private lazy var cicrleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //房子
    private lazy var largeHouse: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var tioLabel: UILabel = UIFactory.labelFactory(UIColor.darkGrayColor(), fontSize: 15, text: "")
    
    
    //.登录和注册按钮
    private lazy var loginBtn: UIButton = UIFactory.buttonWithtitle("登录", backImageName: "common_button_white_disable", textColor: themCocor, fontSize: 14)
    
    
        
    private lazy var registerBtn: UIButton = UIFactory.buttonWithtitle("注册", backImageName: "common_button_white_disable", textColor: themCocor, fontSize: 14)
    
    
    
}
